//@原文链接 https://blog.csdn.net/ITSOK_123/article/details/123915176

//暴力解法
class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int n = nums.size();
        int sublength = n;
        int result = n+1;
        for(int i=0;i<n;i++){
            int sums = 0;
            for(int j=i;j<n;j++){
                sums+=nums[j];
                if(sums>=target){
                    sublength = j-i+1;
                    result = result<sublength?result:sublength;
                    break;
                }
            }
        }
        return result==n+1?0:result;
    }
};


//双指针法——滑动窗口
class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int n = nums.size();
        int sublength = n;
        int result = n+1;
        int sum = 0;
        int i=0;
        for(int j=0;j<n;j++){
            sum+=nums[j];
            //滑动窗口的移动，在这里只移动左端，因为右端做循环操作
            while(sum>=target){
                sublength = j-i+1;
                result = result<sublength?result:sublength;
                sum -= nums[i++];
            }
        }
        return result==n+1?0:result;
    }
};
